﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Logis.Utility.Log;
using Logis.Kernel;
using LBSPlatform.LBSDispatch.WaiterClientLib;
namespace TTTelIncoming
{
    public partial class IVRForm : Form
    {
        public IVRForm()
        {
            InitializeComponent();
        }

        public void InitialIVRServer()
        {
            LogManager.getInstance().initial("IVR");
            IVRTalker.getInstance().initial();
            TIServerManager.getInstance().initialListen();
            TIServerManager.getInstance().initialReceive();
            string str = System.DateTime.Now.ToString("yyyyMMddHHmmss");
        }

        private void button_Start_Click(object sender, EventArgs e)
        {
            InitialIVRServer();

            this.timer_Refresh.Enabled = true;
            
        }

        private void button_Refresh_Click(object sender, EventArgs e)
        {
            string[] strary = IVRTalker.getInstance().getLog();

            foreach (string str in strary)
            {
                this.listBox_Log.Items.Insert(0, str);
            }

            while (this.listBox_Log.Items.Count > 5000)
            {
                this.listBox_Log.Items.RemoveAt(this.listBox_Log.Items.Count - 1);
            }
        }

        private void button_CallOut_Click(object sender, EventArgs e)
        {

            IVRTalker.getInstance().CallOutSend(this.textBox1.Text, "1090218032943000236");
        }

        private void timer_Refresh_Tick(object sender, EventArgs e)
        {
            this.button_Refresh_Click(null, null);
        }

        private void IVRForm_FormClosed(object sender, FormClosedEventArgs e)
        {
           
        }


        bool m_bClosed = false;
        private bool closeServer()
        {
            if (m_bClosed)
                return true;
            if (MessageBox.Show("確定要關閉IVR接收伺服器..", "確認關閉?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                m_bClosed = true;
                this.timer_Refresh.Enabled = false;
                
                IVRTalker.getInstance().shutdown();
                TIServerManager.getInstance().shutdown();
                
                return true;
            }

            return false;

        }

        private void button_SendTestToDC_Click(object sender, EventArgs e)
        {
            IVRTalker.getInstance().sendToDClient("0100","001", "0921305001","M_KeyNG","");

            IVRTalker.getInstance().sendToDClient("0100", "003", "0921305003", "M_SQ", "");

            IVRTalker.getInstance().sendToDClient("0100", "005", "0921305004", "C_AckTout1", "");


        }

        private void button_SendTestToDC2_Click(object sender, EventArgs e)
        {
            IVRTalker.getInstance().sendToDClient("0100", "001", "", "", "");

            IVRTalker.getInstance().sendToDClient("0100", "003", "", "", "");

            IVRTalker.getInstance().sendToDClient("0100", "002", "", "", "");

            IVRTalker.getInstance().sendToDClient("0100", "012", "", "", "");

        }

        private void button_StopResponse_Click(object sender, EventArgs e)
        {
            IVRTalker.getInstance().m_bStopResponse = !IVRTalker.getInstance().m_bStopResponse;

            if (IVRTalker.getInstance().m_bStopResponse)
            {
                this.button_StopResponse.Text = "恢復回應";
            }
            else
            {
                this.button_StopResponse.Text = "暫停回應";
            }
        }

        private void checkBox_Send09ToIVR_CheckedChanged(object sender, EventArgs e)
        {
            TIServerManager.getInstance().setIsSend09ToIVR(this.checkBox_Send09ToIVR.Checked);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IVRTalker.getInstance().processCMD(this.textBox1.Text);
        }

        private void button_SendOutDial_Click(object sender, EventArgs e)
        {
            //jobid=yyyymmddhhnnsstttt055688&state=AN&callerid=nnnn&op=ext&time=yyyymmddhhnnss/
            string strTemp = "jobid={0}&state=CON&callerid={1}&TaxiOID={2}&ResultType={3}&TaxiResult={4}";
            string[] strData = this.textBox_TTIVRData.Text.Split(';');

            string strSend = string.Format(strTemp, System.DateTime.Now.ToString("yyyyMMddHHmmss") + strData[0]+"000000", strData[1], strData[2],strData[3], strData[4].Replace(',', '\t'));
            IVRTalker.getInstance().addRunLog("PS CON:" + strSend);
            IVRTalker.getInstance().sendToTTIVR(strSend);

        }

        private void button_IVRReturn_Click(object sender, EventArgs e)
        {

            string strTemp = "jobid={0}&state=DPR&callerid={1}&TaxiOID={2}&TaxiNeedCar={3}&ResultType={4}&TaxiResult={5}";
            string[]  strData = this.textBox_TTIVRData2.Text.Split(';');
            string strSend = string.Format(strTemp, System.DateTime.Now.ToString("yyyyMMddHHmmss") + strData[0] + "000000", strData[1], strData[2], strData[3], strData[4],strData[5].Replace(',', '\t'));
            IVRTalker.getInstance().addRunLog("PS DPR:" + strSend);
            IVRTalker.getInstance().sendToTTIVR(strSend);




        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strTemp = "jobid={0}&state=RWD&op={1}";
            string[] strData = this.textBox_IVRData3.Text.Split(';');
            string strSend = string.Format(strTemp, System.DateTime.Now.ToString("yyyyMMddHHmmss") + strData[0] + "000000", strData[1]);
            IVRTalker.getInstance().addRunLog("PS RWD:" + strSend);
            IVRTalker.getInstance().sendToTTIVR(strSend);

        }

        private void IVRForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            bool bResult = closeServer();
            if (!bResult)
                e.Cancel = true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            TIServerManager.getInstance().sendToClient("!U\t009\t" + System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "\t0921305472\t" + "專人叫車回應無車" + "\t" + "");

        }
    }
}